package cn.elricyu.kf.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import cn.elricyu.kf.util.DateUtil;
import cn.elricyu.kf.util.KeyGenerater;

import com.google.appengine.repackaged.com.google.common.util.Base64;

@Entity(name = "RegInfo")
@Table(name = "RegInfo")
@NamedQueries({
		@NamedQuery(name = "RegInfo.findRegInfoByUuid", query = "SELECT farm "
				+ "FROM RegInfo farm " + "WHERE farm.uuid = :uuid"),
		@NamedQuery(name = "RegInfo.findRegInfoByTbId", query = "SELECT farm "
				+ "FROM RegInfo farm " + "WHERE farm.tbId = :tbId"),
		@NamedQuery(name = "RegInfo.findRegInfoBySncode", query = "SELECT farm "
				+ "FROM RegInfo farm WHERE farm.snCode = :snCode"),
		@NamedQuery(name = "RegInfo.getRegInfoBySite", query = "SELECT farm "
				+ "FROM RegInfo farm WHERE farm.site = :site ORDER BY gmtEnd desc"),
		@NamedQuery(name = "RegInfo.hasBindType", query = "SELECT farm "
				+ "FROM RegInfo farm WHERE farm.sntype = :sntype and farm.appName = :appName"
				+ " and farm.site = :site and farm.bindId = :bindId"),
		@NamedQuery(name = "RegInfo.findRegInfo", query = "SELECT farm FROM RegInfo farm "
				+ " WHERE farm.snCode = :snCode and farm.site = :site and farm.bindId = :bindId"),
		@NamedQuery(name = "RegInfo.getAllRegInfo", query = "SELECT farms "
				+ "FROM RegInfo farms ORDER BY gmtEnd desc") })
public class RegInfo {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private Long id;

	@Column(name = "uuid", nullable = false, length = 36)
	private String uuid;

	@Column(name = "tbId", nullable = true, length = 20)
	private String tbId;

	@Column(name = "apId", nullable = true, length = 20)
	private String apId;

	@Column(name = "sntype", nullable = false, length = 36)
	private String sntype;

	@Column(name = "snCode", nullable = true, length = 300)
	private String snCode;

	@Column(name = "appName", nullable = true, length = 30)
	private String appName;

	@Column(name = "site", nullable = true, length = 20)
	private String site;

	@Column(name = "bindId", nullable = true, length = 40)
	private String bindId;

	@Column(name = "allowCount")
	private int allowCount;

	@Column(name = "gmtStart")
	private long gmtStart;

	@Column(name = "gmtEnd")
	private long gmtEnd;

	@Column(name = "gmtCreate")
	private long gmtCreate = System.currentTimeMillis();

	@Column(name = "gmtUpdate")
	private long gmtUpdate;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getTbId() {
		return tbId;
	}

	public void setTbId(String tbId) {
		this.tbId = tbId;
	}

	public String getApId() {
		return apId;
	}

	public void setApId(String apId) {
		this.apId = apId;
	}

	public String getSnCode() {
		return snCode;
	}

	public void setSnCode(String snCode) {
		this.snCode = snCode;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public String getBindId() {
		return bindId;
	}

	public void setBindId(String bindId) {
		this.bindId = bindId;
	}

	public int getAllowCount() {
		return allowCount;
	}

	public void setAllowCount(int allowCount) {
		this.allowCount = allowCount;
	}

	public String getSntype() {
		return sntype;
	}

	public void setSntype(String sntype) {
		this.sntype = sntype;
	}

	public long getGmtStart() {
		return gmtStart;
	}

	public void setGmtStart(long gmtStart) {
		this.gmtStart = gmtStart;
	}

	public long getGmtEnd() {
		return gmtEnd;
	}

	public void setGmtEnd(long gmtEnd) {
		this.gmtEnd = gmtEnd;
	}

	public long getGmtCreate() {
		return gmtCreate;
	}

	public void setGmtCreate(long gmtCreate) {
		this.gmtCreate = gmtCreate;
	}

	public long getGmtUpdate() {
		return gmtUpdate;
	}

	public void setGmtUpdate(long gmtUpdate) {
		this.gmtUpdate = gmtUpdate;
	}

	public String getGmtStartDesc() {
		if (gmtStart == 0) {
			return "";
		}
		return DateUtil.simpleFormat(gmtStart);
	}

	public String getGmtEndDesc() {
		if (gmtEnd == 0) {
			return "";
		}
		return DateUtil.simpleFormat(gmtEnd);
	}

	public String getGmtUpdateDesc() {
		if (gmtUpdate == 0) {
			return "";
		}
		return DateUtil.simpleFormat(gmtUpdate);
	}

	public String getGmtCreateDesc() {
		if (gmtCreate == 0) {
			return "";
		}
		return DateUtil.simpleFormat(gmtCreate);
	}

	public String generateCode() {
		List<Integer> integerList = new ArrayList<Integer>(7);
		String[] valueArray = new String[7];
		for (int i = 0; i < 7; i++) {
			integerList.add(i);
			valueArray[i] = "";
		}
		Collections.shuffle(integerList);
		StringBuilder sb = new StringBuilder();
		String order = "";
		for (int j = 0; j < integerList.size(); j++) {
			Integer integer = integerList.get(j);
			valueArray[integer] = getString(j);
			order += integer;
		}
		for (String string : valueArray) {
			sb.append(string);
			sb.append(KeyGenerater.SEPERATOR);
		}
		sb.append(order);
		return sb.toString();
	}

	public String getString(int postion) {
		String value = "";
		switch (postion) {
		case 0:
			value = uuid;
			break;
		case 1:
			value = appName;
			break;
		case 2:
			value = site;
			break;
		case 3:
			value = Base64Encode(bindId);
			break;
		case 4:
			value = allowCount + "";
			break;
		case 5:
			value = gmtStart + "";
			break;
		case 6:
			value = gmtEnd + "";
			break;
		default:
			break;
		}
		return value;
	}

	@Override
	public RegInfo clone() {
		RegInfo newInfo = new RegInfo();
		newInfo.setAppName(appName);
		newInfo.setApId(apId);
		newInfo.setAllowCount(allowCount);
		newInfo.setGmtStart(gmtStart);
		newInfo.setGmtEnd(gmtEnd);
		newInfo.setGmtCreate(System.currentTimeMillis());
		newInfo.setGmtUpdate(System.currentTimeMillis());
		newInfo.setSntype(sntype);
		newInfo.setTbId(tbId);
		newInfo.setUuid(uuid);
		return newInfo;
	}

	private String Base64Encode(String str) {
		try {
			return Base64.encode(str.getBytes("utf-8"));
		} catch (Exception e) {
			return str;
		}
	}

	public static void main(String[] args) {
		RegInfo info = new RegInfo();
		info.setUuid("uuid");
		info.setAppName("taitan");
		info.setGmtStart(System.currentTimeMillis());
		info.setGmtEnd(System.currentTimeMillis()+10000000l);
		info.setSite("site");
		info.setAllowCount(5);
		info.setBindId("bindId");
		System.err.println(info.generateCode());
		String snCode = KeyGenerater.generateSNCode(info.generateCode());
		System.err.println(snCode);
	}
}
